package com.feature50.makeover.ui;

import com.feature50.makeover.utils.FontResolver;
import com.feature50.makeover.utils.Platform;
import com.jgoodies.forms.factories.Borders;

import javax.swing.*;
import javax.swing.plaf.basic.BasicComboBoxUI;
import java.awt.*;

public class MiniBlackComboUI extends BasicComboBoxUI {
    private final Color color = new Color(138, 138, 138);

    protected JButton createArrowButton() {
        return new ArrowButton();
    }

    protected void installDefaults() {
        super.installDefaults();
        comboBox.setOpaque(true);
        comboBox.setFont(FontResolver.getFont(new FontResolver.FontInfo("LucidaGrandeBold", 9, true, true)));
        comboBox.setBorder(Borders.DLU2_BORDER);
    }

    public Dimension getMinimumSize( JComponent c ) {
        if (!isMinimumSizeDirty) {
            return new Dimension(cachedMinimumSize);
        }

        Dimension size = getDisplaySize();
        Insets insets = getInsets();
        size.height += insets.top + insets.bottom;

        Dimension bs = arrowButton.getPreferredSize();
        if (size.height < bs.getHeight()) size.height = (int) bs.getHeight();

        size.width += insets.left + insets.right + bs.getWidth() + 5;

        cachedMinimumSize.setSize( size.width, size.height );
        isMinimumSizeDirty = false;

        return new Dimension(size);
    }

    protected LayoutManager createLayoutManager() {
        return new LayoutManager() {
            public void addLayoutComponent(String name, Component comp) {}
            public void removeLayoutComponent(Component comp) {}

            public Dimension preferredLayoutSize(Container parent) {
                return parent.getPreferredSize();
            }

            public Dimension minimumLayoutSize(Container parent) {
                return parent.getMinimumSize();
            }

            public void layoutContainer(Container parent) {
                JComboBox cb = (JComboBox) parent;
                int width = cb.getWidth();
                int height = parent.getHeight();

                if (arrowButton != null) {
                    arrowButton.setBounds(width - arrowButton.getPreferredSize().width, 0, arrowButton.getPreferredSize().width, height);
                }

                if (editor != null) {
                    Rectangle cvb = rectangleForCurrentValue();
                    editor.setBounds(cvb);
                }
            }
        };
    }

    public void paint(Graphics g, JComponent c) {
        Graphics2D g2d = (Graphics2D) g.create();

        int w = c.getWidth();
        int h = c.getHeight();

        g2d.setColor(Color.BLACK);
        g2d.fillRect(0, 0, w, h);

        if (comboBox.getSelectedItem() != null) {
            String text = comboBox.getSelectedItem().toString();
            g2d.setColor(color);
            g2d.setFont(c.getFont());
            int x = 5;
            int ascent = g2d.getFontMetrics().getAscent();
            int y = (c.getHeight() / 2) + (ascent / 2);
            Platform.setAntialiasing(g2d, g2d.getFont());
            g2d.drawString(text, x, y);
        }

        g2d.setPaint(new GradientPaint(0, 0, new Color(99, 99, 99), 0, h, new Color(41, 41, 41)));
        g2d.drawRect(0, 0, w - 1, h - 1);

        g2d.dispose();
    }

    class ArrowButton extends JButton {
        private final int width = 7;
        private final int height = 4;

        public ArrowButton() {
            setOpaque(false);
            setBorder(null);
        }

        public Dimension getPreferredSize() {
            return new Dimension(12, 8);
        }

        public Dimension getMinimumSize() {
            return getPreferredSize();
        }

        public Dimension getMaximumSize() {
            return getPreferredSize();
        }

        protected void paintComponent(Graphics g) {
            Graphics2D g2d = (Graphics2D) g.create();

            int x = (getWidth() - width) / 2;
            int y = (getHeight() - height) / 2;

            int midx = x + (width / 2);

            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.setColor(color);
            g2d.fillPolygon(
                    new int[] { x, x + width, midx },
                    new int[] { y, y, y + height },
                    3
            );

            g2d.dispose();
        }
    }
}